﻿'''
''' Programmed by: Muhammad Yasin
''' Email: Muhammad-Yasin@outlook.com
''' 
''' <summary>
''' It gets the data needed by the program. It's the backbone of the program.
''' </summary>
''' <remarks></remarks>
Module Core
#Region "Variables"
    Public TempFileName As String
    'Some Customizations
    Public City As Integer = 0
    Public MyColor As Color = Color.Black
    Public Volume As Integer
    'Color presets
    Public Colors() As Color = {Color.RoyalBlue, Color.FromArgb(243, 55, 55), _
                              Color.FromArgb(249, 0, 165), Color.FromArgb(0, 176, 80), _
                             Color.FromArgb(160, 20, 180)}

    'Uncategorized Variables :)
    Public NxtPrayer As Integer = 6
    Dim DataFile As New Ini 'Define a data file
    Public Prayers As String() = {"بەیانی", "گزنگ", "نیوەڕۆ", "عەسر", "شێوان", "خەوتنان"}
    Public Cities As String() = {"هەولێر", "كەركوك", "سلێمانی", "دهۆك", "هەڵەبجە"}
    Dim CitiesE As String() = {"Hawler", "Kirkuk", "Sulaymani", "Duhok", "Sulaymani"}
    Public BangbezhFile As String
    Public UseTray As Boolean = False
    Public Fix As Boolean
    Public FixMins As TimeSpan

    'Bangbezh on the internet
    Public Website As String = "http://bangbezh.com"
    Public FacebookPage As String = "http://www.facebook.com/bangbezh"
    Public ProjectHost As String = "http://bangbezh.codeplex.com"
    Public Email As String = "Bangbezh@outlook.com"
    'Remaining time to...
    Public Rem2Fajr As Integer 'Remaining time to Fajr in seconds.
    Public Rem2Shruq As Integer 'Remaining time to Shruq in seconds.
    Public Rem2Dhuhr As Integer 'Remaining time to Dhuhr............
    Public Rem2Asr As Integer '................. Asr .............
    Public Rem2Maghrib As Integer '................. Maghrib..........
    Public Rem2Isha As Integer '................. Isha ............
    Public Rem2PrNxt As Integer '..............Next Prayer........
    'Maximum Times
    Public MaxFajr As Integer 'Time Difference between Isha and Fajr
    Public MaxShruq As Integer 'Time Difference between Fajr and Shruq
    Public MaxDhuhr As Integer
    Public MaxAsr As Integer
    Public MaxMaghrib As Integer
    Public MaxIsha As Integer
    Public MaxPrNxt As Integer
    'Declare a variable for the Prayers with default time
    Public Fajr As String = "6:02 AM"
    Public Shruq As String = "7:21 AM"
    Public Dhuhr As String = "12:16 PM"
    Public Asr As String = "2:45 PM"
    Public Maghrib As String = "5:06 PM"
    Public Isha As String = "7:00 PM"
    'If Halabja
    Public Halabja As Boolean = False
    'Prayers in Date format
    Public FajrD As Date
    Public ShruqD As Date
    Public DhuhrD As Date
    Public AsrD As Date
    Public MaghribD As Date
    Public IshaD As Date
    Public PrNxtD As Date 'Next prayer

#End Region
    ''' <summary>
    ''' Get the Date differences between the prayers and this moment, prayers and prayers etc...
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Gtdff()
        '-------Getting nearest MidNight (according to now)-------
        'This is a bit hard task, because if you tell it to get next 12 AM
        'Then if it is for example 1 AM it'll give you 12 AM of that
        'Day, which means more than 16 hours past Isha.
        'To do this we declare two variables.
        'First one is TomMidnight or "Tomorrow Midnight" and it's tomorrow's midnight
        'Second one is NowMidnight  and it's today's midnight (to be easier for our eyes
        'to distinguish between the two variables, we named the second one NowMidnight not
        'Todmidnight or "Today Midnight")

        Dim Midnight As Date 'Declare a virable for midnight
        Dim tomMidnight As Date = Date.Parse("12:00 AM").AddDays(1)  'Declare a variable for tomorrow's midnight
        Dim nowMidnight As Date = Date.Parse("12:00 AM")             'Declare a variable for today's midnight

        'If time between Isha and today's midnight < 10 hours (when time is less than 11:59 PM) then
        If DateDiff(DateInterval.Hour, nowMidnight, Date.Parse(Isha)) < 10 Then
            Midnight = nowMidnight 'We use today's midnight
        Else 'Else
            Midnight = tomMidnight 'We use tomorrow' midnight
        End If
        '''''''''''''''''''''''''''FAJR'''''''''''''''''''''''
        If Now > Date.Parse(Isha) And NxtPrayer = 0 And Now < Midnight Then
            If Halabja Then
                FajrD = Date.Parse(Fajr).AddDays(1).AddMinutes(-2)  'If It's Halabja, then subtract two minutes from it.
            Else
                FajrD = Date.Parse(Fajr).AddDays(1) 'Get today's Fajr time, then parse it to date format (because it's saved as string)
            End If
        Else
            If Halabja Then
                FajrD = Date.Parse(Fajr).AddMinutes(-2) 'If it's Halabja, -2 minutes
            Else
                FajrD = Date.Parse(Fajr) 'Get today's Fajr time, then parse it to date format (because it's saved as string)
            End If
        End If
        'Get the difference between now and next fajr.
        Dim differenceInSecondsF As Long = DateDiff(DateInterval.Second, Now, FajrD)
        Rem2Fajr = CInt(differenceInSecondsF)
        '''''''''''''''''SHRUQ'''''''''''''''''''''''''''
        ShruqD = Date.Parse(Shruq)
        If Now > Date.Parse(Maghrib) And Now < Midnight Then
            ShruqD = Date.Parse(Shruq).AddDays(1) 'Get today's Shruq time, then parse it to date format (because it's saved as string)
        Else
            ShruqD = Date.Parse(Shruq) 'Get today's Shruq time, then parse it to date format (because it's saved as string)
        End If
        'Get the difference between now and next shruq.
        Dim differenceInSecondsS As Long = DateDiff(DateInterval.Second, Now, ShruqD)
        Rem2Shruq = CInt(differenceInSecondsS)
        ''''''''''''''''Dhuhr'''''''''''''''''''
        DhuhrD = Date.Parse(Dhuhr)
        Dim differenceInSecondsD As Long = DateDiff(DateInterval.Second, Now, DhuhrD)
        Rem2Dhuhr = CInt(differenceInSecondsD)
        ''''''''''''''''ASR'''''''''''''''''
        AsrD = Date.Parse(Asr)
        Dim differenceInSecondsA As Long = DateDiff(DateInterval.Second, Now, AsrD)
        Rem2Asr = CInt(differenceInSecondsA)
        ''''''''''''''''MAGHRIB'''''''''''''''''
        If Halabja Then
            'If It's Halabja, then subtract two minutes from it.
            MaghribD = Date.Parse(Maghrib).AddMinutes(-2)
        Else
            MaghribD = Date.Parse(Maghrib)
        End If

        Dim differenceInSecondsM As Long = DateDiff(DateInterval.Second, Now, MaghribD)
        Rem2Maghrib = CInt(differenceInSecondsM)

        '''''''''''''''ISHA'''''''''''''''''''''''
        IshaD = Date.Parse(Isha)
        Dim differenceInSecondsI As Long = DateDiff(DateInterval.Second, Now, IshaD)
        Rem2Isha = CInt(differenceInSecondsI)

        '''''''''''''Max Fajr'''''''''''''''''''''''
        'Get the difference between Isha and fajr.
        Dim differenceInSecondsFM As Long = DateDiff(DateInterval.Second, IshaD, FajrD)
        MaxFajr = CInt(differenceInSecondsFM)
        '''''''''''''Max Shruq'''''''''''''''''''''''
        'Get the difference between Fajr and Shruq.
        Dim differenceInSecondsSM As Long = DateDiff(DateInterval.Second, MaghribD, ShruqD)
        MaxShruq = CInt(differenceInSecondsSM)
        '''''''''''''Max Dhr'''''''''''''''''''''''
        'Get the difference between Fajr and Dhr.
        Dim differenceInSecondsDM As Long = DateDiff(DateInterval.Second, FajrD, DhuhrD)
        MaxDhuhr = CInt(differenceInSecondsDM)
        '''''''''''''Max Asr'''''''''''''''''''''''
        'Get the difference between Dhr and Asr.
        Dim differenceInSecondsAM As Long = DateDiff(DateInterval.Second, DhuhrD, AsrD)
        MaxAsr = CInt(differenceInSecondsAM)
        '''''''''''''Max Maghrib'''''''''''''''''''''''
        'Get the difference between Asr and Maghrib.
        Dim differenceInSecondsMM As Long = DateDiff(DateInterval.Second, AsrD, MaghribD)
        MaxMaghrib = CInt(differenceInSecondsMM)
        '''''''''''''Max Isha'''''''''''''''''''''''
        'Get the difference between Maghrib and Isha.
        Dim differenceInSecondsIM As Long = DateDiff(DateInterval.Second, MaghribD, IshaD)
        MaxIsha = CInt(differenceInSecondsIM)

        Select Case NxtPrayer
            Case 0
                PrNxtD = FajrD.AddMinutes(FixMins.TotalSeconds)
                MaxPrNxt = CInt(MaxFajr - FixMins.TotalSeconds)
                Rem2PrNxt = CInt(Rem2Fajr - FixMins.TotalSeconds)
            Case 1
                PrNxtD = DhuhrD.AddMinutes(FixMins.TotalSeconds)
                MaxPrNxt = CInt(MaxDhuhr - FixMins.TotalSeconds)
                Rem2PrNxt = CInt(Rem2Dhuhr - FixMins.TotalSeconds)
                NxtPrayer = 2
            Case 2
                PrNxtD = DhuhrD.AddMinutes(FixMins.TotalSeconds)
                MaxPrNxt = CInt(MaxDhuhr - FixMins.TotalSeconds)
                Rem2PrNxt = CInt(Rem2Dhuhr - FixMins.TotalSeconds)
            Case 3
                PrNxtD = AsrD.AddMinutes(FixMins.TotalSeconds)
                MaxPrNxt = CInt(MaxAsr - FixMins.TotalSeconds)
                Rem2PrNxt = CInt(Rem2Asr - FixMins.TotalSeconds)
            Case 4
                PrNxtD = MaghribD.AddMinutes(FixMins.TotalSeconds)
                MaxPrNxt = CInt(MaxMaghrib - FixMins.TotalSeconds)
                Rem2PrNxt = CInt(Rem2Maghrib - FixMins.TotalSeconds)
            Case 5
                PrNxtD = IshaD.AddMinutes(FixMins.TotalSeconds)
                MaxPrNxt = CInt(MaxIsha - FixMins.TotalSeconds)
                Rem2PrNxt = CInt(Rem2Isha - FixMins.TotalSeconds)
        End Select
    End Sub
    ''' <summary>
    ''' Get today's Prayer times.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub GetPrayerTimes()
        Try
            'Get today's section
            Dim today As String = ChangeFormat(Now.Month) & ";" & ChangeFormat(Now.Day)
            Fajr = DataFile.GetStringField(today, "Fajr") & " AM"
            Shruq = DataFile.GetStringField(today, "Shruq") & " AM"
            If Date.Parse(DataFile.GetStringField(today, "Dhuhr")).Hour > 11 Then
                Dhuhr = DataFile.GetStringField(today, "Dhuhr") & " PM"
            Else
                Dhuhr = DataFile.GetStringField(today, "Dhuhr") & " AM"
            End If
            Asr = DataFile.GetStringField(today, "Asr") & " PM"
            Maghrib = DataFile.GetStringField(today, "Maghrib") & " PM"
            Isha = DataFile.GetStringField(today, "Isha") & " PM"

        Catch ex As Exception
            'If error occured then show a messagebox
            FrmMain.TmrPrayers.Enabled = False
            MessageBox.Show("‌هەڵەیەك ڕوویدا لە كاتی هەوڵدان بۆ بەدەستهێنانی كاتی بانگەكان: " & ex.Message, "هەڵەی #02", MessageBoxButtons.OK)
        End Try

    End Sub
    ''' <summary>
    ''' Gives you the prayer times for the given date.
    ''' </summary>
    ''' <param name="_Date">The Date of which the prayer times are asked for.</param>
    ''' <returns>String()</returns>
    ''' <remarks></remarks>
    Public Function PrayerTimes(_Date As Date) As String()
        Dim _PrayerTimes As String()
        Try
            'Get today's section
            Dim today As String = ChangeFormat(_Date.Month) & ";" & ChangeFormat(_Date.Day)
            _PrayerTimes = {HinduNumeric(_Date.Day.ToString), FixThis(DataFile.GetStringField(today, "Fajr")), _
           FixThis(DataFile.GetStringField(today, "Shruq")), FixThis(DataFile.GetStringField(today, "Dhuhr")), _
            FixThis(DataFile.GetStringField(today, "Asr")), FixThis(DataFile.GetStringField(today, "Maghrib")), _
            FixThis(DataFile.GetStringField(today, "Isha"))}

            Return _PrayerTimes
        Catch ex As Exception
            'If error occured then show a messagebox
            FrmMain.TmrPrayers.Enabled = False
            MessageBox.Show("‌هەڵەیەك ڕوویدا لە كاتی هەوڵدان بۆ بەدەستهێنانی كاتی بانگەكان: " & ex.Message, "هەڵەی #02", MessageBoxButtons.OK)
        End Try

    End Function
    Public Function FixThis(_Date As String) As String
        If Halabja Then
            Return New String(CType(HinduNumeric(Date.Parse(_Date).AddMinutes(-2).ToString("hh:mm")), Char()))
        Else
            Return New String(CType(HinduNumeric(Date.Parse(_Date).ToString("hh:mm")), Char()))
        End If
    End Function
    ''' <summary>
    ''' Loads The Data file.
    ''' </summary>
    ''' <remarks></remarks>
    Sub Load()
        Dim CityName As String
        If City = 4 Then Halabja = True Else Halabja = False

        Try
            CityName = CitiesE(City)
            DataFile.Load(Application.StartupPath & "\Data\KatiBangakan\" & CityName & ".Bbj")
        Catch ex As IndexOutOfRangeException
            CityName = CitiesE(0)
            WriteOptions()
            MsgBox("هەڵەیەك ڕوویدا لەكاتی هەوڵدان بۆ دەستنیشانكردنی ئەو پارێزگایەی كە تۆ تێیدا نیشتەجێی، ڕێكخستنەكان داندرانەوە.تکایە بانگبێژ دووبارە دەستپێبکەوە.", MsgBoxStyle.Exclamation, "هەڵەی #03")
            Application.Exit()
        End Try
       
    End Sub
    ''' <summary>
    ''' Get the Next prayer.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub GetNextPrayer()
        GetPrayerTimes()
        Gtdff() 'Get the remaining time to the prayers

        'Do some calculations :D
        If Rem2Fajr > 0 Then 'IF Time difference between Fajr and current moment was greater than 0 seconds
            If Rem2Fajr < Rem2Shruq Then 'If remaining time of Fajr was less than the remaining time of Dhuhr
                NxtPrayer = 0 'Then the next prayer is Fajr
            End If
        End If

        If Rem2Shruq > 0 Then 'Same story...
            If Rem2Shruq < Rem2Dhuhr Then
                If Rem2Fajr <= 0 Then
                    NxtPrayer = 1
                End If
            End If
        End If

        If Rem2Dhuhr > 0 Then  'Same story...
            If Rem2Dhuhr < Rem2Asr Then
                If Rem2Shruq <= 0 Then
                    NxtPrayer = 2
                End If
            End If
        End If

        If Rem2Asr > 0 Then 'Same story...
            If Rem2Asr < Rem2Maghrib Then
                If Rem2Dhuhr <= 0 Then
                    NxtPrayer = 3
                End If
            End If
        End If

        If Rem2Maghrib > 0 Then 'Same story...
            If Rem2Maghrib < Rem2Isha Then
                If Rem2Asr <= 0 Then
                    NxtPrayer = 4
                End If
            End If
        End If


        If Rem2Isha > 0 Then 'Same story...
            If Rem2Isha >= Rem2Fajr Then
                If Rem2Maghrib <= 0 Then
                    NxtPrayer = 5
                End If
            End If
        End If

        If Rem2Isha < 0 Then 'After Time difference of Isha becomes less than 0 seconds, change back the next prayer to Fajr.
            NxtPrayer = 0
        End If

    End Sub
    ''' <summary>
    ''' This function is used to correct the format of the numbers
    ''' for example it changes (5 > 05) to make a standard format for
    ''' all of the numbers.
    ''' </summary>
    ''' <param name="number">The number needed to change</param>
    ''' <returns>The number with a standard format</returns>
    ''' <remarks></remarks>
    Public Function ChangeFormat(ByVal number As Integer) As String
        Dim NewNumber As String
        If number < 10 Then
            NewNumber = "0" & number
        Else
            NewNumber = number.ToString
        End If
        Return NewNumber
    End Function
    ''' <summary>
    ''' Inform the user that it's time for prayer.
    ''' </summary>
    ''' <remarks></remarks>
    Sub Bangda()
        FrmCall.Show()
        Select Case NxtPrayer
            Case 5
                NxtPrayer = 0
            Case 1
                NxtPrayer = 2
            Case Else
                NxtPrayer += 1
        End Select

        'Set TmrPrayers interval to 1000 so that it doesn't call the Bangbezh() than once.
        FrmMain.TmrPrayers.Interval = 1000

    End Sub
    ''' <summary>
    ''' Changes Arabic Number to Hindu Number
    ''' </summary>
    ''' <param name="Number"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function HinduNumeric(ByVal Number As String) As String
        Dim HNumber As String
        HNumber = Number.Replace("0", "٠").Replace("1", "١").Replace("2", "٢").Replace("3", "٣").Replace("4", "٤") _
        .Replace("5", "٥").Replace("6", "٦").Replace("7", "٧").Replace("8", "٨").Replace("9", "٩")
        Return HNumber
    End Function
    Public Sub Check()
        If My.Computer.FileSystem.FileExists(Application.StartupPath & "\Data\Options.Bbj") = False Then
            WriteOptions()
        End If

        Dim Content As String = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Data\Options.Bbj")
        If Content.Contains("City") = False Or Content.Contains("Color") = False Or Content.Contains("Volume") = False _
            Or Content.Contains("Bangbezh") = False Or Content.Contains("Startup") = False Or Content.Contains("Zikr") = False _
            Or Content.Contains("Fix") = False Or Content.Contains("FixMins") = False Then
            WriteOptions()
        End If
    End Sub
    Sub WriteOptions()
        My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\Data\Options.Bbj", My.Resources.Options, False)
    End Sub
    Public Function GetStartupPath() As String
        Return Application.StartupPath
    End Function
End Module

